package com.lishu.carsafe.activity;

import android.annotation.SuppressLint;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.baidu.location.BDAbstractLocationListener;
import com.baidu.location.BDLocation;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.map.PolylineOptions;
import com.baidu.mapapi.map.TextOptions;
import com.baidu.mapapi.map.UiSettings;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.route.RoutePlanSearch;
import com.lishu.carsafe.R;
import com.lishu.carsafe.http.HttpBase;
import com.lishu.carsafe.http.HttpLoader;
import com.lishu.carsafe.model.BaseModel;
import com.lishu.carsafe.model.ScheduleDetailModel;
import com.lishu.carsafe.model.ScheduleDetailModel.ScheduleDetail;
import com.lishu.carsafe.model.entity.Lonlat;
import com.lishu.carsafe.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * describe: 查看地图界面
 *
 * @author:ZhangWei(张伟) date:2020/5/25
 * time:16:21
 * motto:高山仰止，景行行止，虽不能至，心向往之。
 */
public class BDMapActivity extends BaseMapActivity {
    //line id的key
    public static final String LINE_ID = "line_id";
    public static final String LINE_NAME = "line_name";
    private TextView tv_map_dt, tv_map_wx, tv_map_sw;
    private int lineId;
    private String lineName;
    private LatLng center;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bd_map);

        // 申请动态权限
        requestPermission();

        initView();
        initData();
        initEven();
    }

    @Override
    protected void onResume() {
        super.onResume();
        getLinkDetail();
    }

    private void initEven() {

    }

    private void initData() {
        lineId = getIntent().getIntExtra(LINE_ID, 0);
        lineName = getIntent().getStringExtra(LINE_NAME);

        setTopTitle(lineName);
        //开启定位图层
        mBaiduMap.setMyLocationEnabled(true);
        initMapLocation();
    }

    @Override
    public void initView() {
        super.initView();
        tv_map_dt = findViewById(R.id.tv_map_dt);
        tv_map_wx = findViewById(R.id.tv_map_wx);
        tv_map_sw = findViewById(R.id.tv_map_sw);

        tv_map_dt.setTextColor(getResources().getColor(R.color.white));
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public void onClickMap(View view) {
        switch (view.getId()) {
            case R.id.tv_map_dt://地图
                changeColor(0);
                //显示普通图层
                mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);
                mUiSettings.setOverlookingGesturesEnabled(false);
                changeLayer(0f);
                break;
            case R.id.tv_map_wx://卫星
                changeColor(1);
                //显示卫星图层
                mBaiduMap.setMapType(BaiduMap.MAP_TYPE_SATELLITE);
                mUiSettings.setOverlookingGesturesEnabled(false);
                changeLayer(0f);
                break;
            case R.id.tv_map_sw://三维
                changeColor(2);
                //显示普通图层
                mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);
                //开启3D
                mUiSettings.setOverlookingGesturesEnabled(true);
                changeLayer(-45.0f);
//                MapStatus.Builder builder = new MapStatus.Builder();
//                float overlook = -45.0f;
//                builder.target(center).overlook(overlook);
//                MapStatusUpdate mapStatusUpdate = MapStatusUpdateFactory.newMapStatus(builder.build());
//                mBaiduMap.setMapStatus(mapStatusUpdate);
                break;
        }
    }

    private void changeLayer(float overlook){
        MapStatus.Builder builder = new MapStatus.Builder();
        builder.target(center).overlook(overlook);
        MapStatusUpdate mapStatusUpdate = MapStatusUpdateFactory.newMapStatus(builder.build());
        mBaiduMap.setMapStatus(mapStatusUpdate);
    }

    @SuppressLint("ResourceAsColor")
    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    private void changeColor(int color) {
        switch (color) {
            case 0:
                tv_map_dt.setTextColor(getResources().getColor(R.color.white));
                tv_map_wx.setTextColor(getResources().getColor(R.color.black));
                tv_map_sw.setTextColor(getResources().getColor(R.color.black));
                tv_map_dt.setBackground(getResources().getDrawable(R.drawable.map_left_bg));
                tv_map_wx.setBackground(null);
                tv_map_sw.setBackground(null);
                break;
            case 1:
                tv_map_wx.setTextColor(getResources().getColor(R.color.white));
                tv_map_dt.setTextColor(getResources().getColor(R.color.black));
                tv_map_sw.setTextColor(getResources().getColor(R.color.black));
                tv_map_dt.setBackground(null);
                tv_map_wx.setBackground(getResources().getDrawable(R.drawable.map_middle_bg));
                tv_map_sw.setBackground(null);
                break;
            case 2:
                tv_map_sw.setTextColor(getResources().getColor(R.color.white));
                tv_map_dt.setTextColor(getResources().getColor(R.color.black));
                tv_map_wx.setTextColor(getResources().getColor(R.color.black));
                tv_map_dt.setBackground(null);
                tv_map_wx.setBackground(null);
                tv_map_sw.setBackground(getResources().getDrawable(R.drawable.map_right_bg));
                break;
        }
    }



    /**
     * 获取路线详情
     */
    private void getLinkDetail() {
        showAlways("正在加载线路数据，请稍后");
        HttpLoader.getScheduleLineDetail(lineId+"", new HttpBase.IResponseListener() {//lineId+"",
            @Override
            public void onResponse(BaseModel model) {
                hide();
                if (model == null) {
                    ToastUtil.showShort(R.string.please_check_network);
                    return;
                }
                if (!model.success()) {
                    ToastUtil.showShort(getString(R.string.get_data_fail) + model.getMsg());
                    return;
                }
                ScheduleDetail detail = ((ScheduleDetailModel) model).getData();
                if(detail != null && detail.getLonlats() != null && detail.getLonlats().size() > 0)
                    center = new LatLng(detail.getLonlats().get(0).getBdLat(), detail.getLonlats().get(0).getBdLon());

                //地图显示出来后 开始画线
                drawPoints(detail.getLonlats());
            }
        });
    }

}
